package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl implements ItemService {
    /*
    汇总数据：
     */
    @Resource
    private ProductFeignClient productFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;
    @Override
    public Map getItem(Long skuId) {
        //  创建map 集合来存储商品详情页面需要的数据
        Map map = new HashMap();
        //  判断
        //        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        //        if (!bloomFilter.contains(skuId)){
        //            //  没有这个skuId 则返回空对象
        //            return map;
        //        }
        //  使用多线程来优化商品详情！

        //  远程调用 获取 skuInfo 数据
        //  分析是有返回值 --> 后续远程调用需要使用 skuInfo 对象中的其他属性数据 ，还是无返回值！
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo",skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        //  查询分类数据
        //  thenApplyAsync 有返回值 return； thenAcceptAsync 没有返回，直接存储到map 就可以了、
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView",categoryView);
        },threadPoolExecutor);

        //  查询最新价格：
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        },threadPoolExecutor);

        //  获取海报信息
        CompletableFuture<Void> spuPosterListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterList = productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
            map.put("spuPosterList", spuPosterList);
        },threadPoolExecutor);
        //  根据skuId 获取平台属性数据
        CompletableFuture<Void> attrCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
            //  只需要attrName, attrValue; 不能直接存储.
            //  第一种方式：修改页面
            //  第二种方式：修改后台代码 skuAttr.attrName; skuAttr.attrValue ----> skuAttr 是实体类 / 或者是map
            List<HashMap<String, String>> mapList = attrList.stream().map(baseAttrInfo -> {
                //  声明一个map 集合
                HashMap<String, String> hashMap = new HashMap<>();
                //  attrName = baseAttrInfo.getAttrName();
                //  attrValue =  baseAttrInfo.getAttrValueList().get(0).getValueName();
                hashMap.put("attrName", baseAttrInfo.getAttrName());
                hashMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return hashMap;
            }).collect(Collectors.toList());

            map.put("skuAttrList",mapList);
        },threadPoolExecutor);


        //  获取销售属性
        CompletableFuture<Void> spuCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrList);
        },threadPoolExecutor);

        //  获取切换功能数据.
        CompletableFuture<Void> jsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = this.productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String jsonStr = JSON.toJSONString(skuValueIdsMap);
            map.put("valuesSkuJson", jsonStr);
        },threadPoolExecutor);

        //  调用热度排名方法.
        CompletableFuture<Void> hotCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);

        //  进行多任务组合
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                priceCompletableFuture,
                spuCompletableFuture,
                spuPosterListCompletableFuture,
                jsonCompletableFuture,
                attrCompletableFuture,
                hotCompletableFuture
        ).join();

        //  返回map 集合数据
        return map;
    }
}
